<?php
if(!defined("IICCMS")) exit("Access Denied");
class TourAction extends BaseAction
{
	public function _initialize() {
		parent::_initialize();
	}
	
    public function index(){
    	$list=array();
    	$tour=M("Tour");
    	$condition=array();
    	$condition['status']=1;
    	$ord = 'id DESC';
		$ord = $_GET ['ord'] == 'new' ? 'createtime DESC' : $ord; //录入先后
		$ord = $_GET ['ord'] == 'last' ? 'lasttime DESC' : $ord; //最后产生点击
		$ord = $_GET ['ord'] == 'modf' ? 'mtime DESC' : $ord; //最后修改
		$ord = $_GET ['ord'] == 'hits' ? 'hits DESC' : $ord;//点击量
		$ord = $_GET ['ord'] == 'random' ? 'rand()' : $ord;//随机
		$ord = $_GET ['ord'] == 'init' ? 'initials DESC' : $ord;//首字母
		//$desc=$_SESSION['desc']=='DESC'?'ASC':'DESC';
		$list=$tour->where($condition)->order($ord)->limit('0,24')->findAll();
    	//dump($tour->getlastsql());
    	$this->assign("list",$list);
    	parent::clickorshow($list,0);
		$this->assign('bcid',0);
        $this->display();
    }
	
	/**
	   *景点列表
	   *@date 2011-7-22
	   *@time 上午08:46:36
	   */
	public function ls() {
		//景点列表
		$catid =  intval($_REQUEST['id']);
		if($catid){
			$p= max(intval($_REQUEST[C('VAR_PAGE')]),1);
			$cat = $this->categorys[$catid];		
			$bcid = explode(",",$cat['arrparentid']); 
			$bcid = $bcid[1];
			if($bcid == '') $bcid=intval($catid);
			$this->assign($cat);
			$cat['id']=$catid;
			$this->assign('catid',$catid);
			$this->assign('bcid',$bcid);
		}
		$condition=array();
    	$condition['status']=1;
    	if($_GET['pid']){
    		$condition['province']=$_GET['pid'];
    		$cat=$this->province[$_GET['pid']];
    		$cat['title']=$cat['province'];
    	}
    	if($_GET['cid']){
    		$condition['city']=$_GET['cid'];
    		$cat=$this->city[$condition['city']];
    		$cat['title']=$cat['city'];
    	}
    	if($_GET['aid']){
    		$condition['area']=$_GET['aid'];
    		$cat=$this->area[$condition['area']];
    		$cat['title']=$cat['area'];
    	}
    	
		$seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
		$this->assign ('seo_title',$seo_title);
		$this->assign ('seo_keywords',$cat['keywords']);
		$this->assign ('seo_description',$cat['description']);
		$list=array();
    	$tour=M("Tour");
    	$ord = '';
		$ord = $_GET ['ord'] == 'new' ? 'createtime DESC' : $ord; //录入先后
		$ord = $_GET ['ord'] == 'last' ? 'lasttime DESC' : $ord; //最后产生点击
		$ord = $_GET ['ord'] == 'modf' ? 'mtime DESC' : $ord; //最后修改
		$ord = $_GET ['ord'] == 'hits' ? 'hits DESC' : $ord;//点击量
		$ord = $_GET ['ord'] == 'random' ? 'rand()' : $ord;//随机
		$ord = $_GET ['ord'] == 'init' ? 'initials DESC' : $ord;//首字母
		import("@.ORG.ZQPage");
		$count = $tour->where($condition)->count();
		$listRows =  !empty($cat['pagesize']) ? $cat['pagesize'] : C('PAGE_LISTROWS');
		$page = new ZQPage($count,$listRows,6);
		$show = $page->show();
		$this->assign('page',$show);
		$list=$tour->where($condition)->order($ord)->limit($page->firstRow.",$listRows")->findAll();
		parent::clickorshow($list,0);
    	$this->assign("list",$list);
    	$this->display();
	}//end ls
	
	/**
	   *景点详细页
	   *@date 2011-7-22
	   *@time 上午11:24:55
	   */
	function detail() {
		//景点详细页
		$p= max(intval($_REQUEST[C('VAR_PAGE')]),1);		
		$id = $id ? $id : intval($_REQUEST['id']);
		$module = $module ? $module : MODULE_NAME;
		$this->dao= M($module);;
		$data = $this->dao->find($id);
		$this->assign("info_id",$id);	
		$catid = $data['catid'];		
		$cat = $this->categorys[$data['catid']];		
		$bcid = explode(",",$cat['arrparentid']); 
		$bcid = $bcid[1]; 
		parent::hits(0);
		if($bcid == '') $bcid=intval($catid);

		$seo_title = $data['title'].'-'.$cat['catname'];
		$this->assign ('seo_title',$seo_title);
		$this->assign ('seo_keywords',$data['keywords']);
		$this->assign ('seo_description',$data['description']);
		
		$fields = F($this->mod[$module].'_Field');
		foreach($data as $key=>$c_d){
			$setup='';
			$fields[$key]['setup'] =$setup=string2array($fields[$key]['setup']);
			if($setup['fieldtype']=='varchar' && $fields[$key]['type']!='text'){
				$data[$key] = implode(',',$data[$key]);
			}elseif($fields[$key]['type']=='images' || $fields[$key]['type']=='files'){ 
				if(!empty($data[$key])){
					$p_data=explode(':::',$data[$key]);
					$data[$key]=array();
					
					foreach($p_data as $k=>$res){
						$p_data_arr=explode('|',$res);					
						$data[$key][$k]['filepath'] = $p_data_arr[0];
						$data[$key][$k]['filename'] = $p_data_arr[1];
					}				
					unset($p_data);
					unset($p_data_arr);
				}
			}
			unset($setup);
		}
		$this->assign('fields',$fields); 
 
		//手动分页
		$CONTENT_POS = strpos($data['content'], '[page]');
		if($CONTENT_POS !== false) {
			
			$urlrule = geturl($cat,$id);
			$urlrule =  str_replace('%7B%24page%7D','{$page}',$urlrule); 
			$contents = array_filter(explode('[page]',$data['content']));
			$pagenumber = count($contents);
			for($i=1; $i<=$pagenumber; $i++) {
				$pageurls[$i] = str_replace('{$page}',$i,$urlrule);
			} 
			$pages = content_pages($pagenumber,$p, $pageurls);
			//判断[page]出现的位置是否在文章开始
			if($CONTENT_POS<7) {
				$data['content'] = $contents[$p];
			} else {
				$data['content'] = $contents[$p-1];
			}
			$this->assign ('pages',$pages);	
		}

		if(!empty($data['template'])){
			$template = $data['template'];
		}elseif(!empty($cat['template_show'])){
			$template = $cat['template_show'];
		}else{
			$template = $module.'_detail';
		}
		
		$link=array();
		$link['pic']=$this->linkdata('Picture',array('tid'=>$id,'catid'=>'37'),'createtime DESC');
		$link['vod']=$this->linkdata('Video',array('tid'=>$id,'catid'=>'38'),'createtime DESC');
		//$link['ser']=$this->linkdata('Article',array('tid'=>$id,'catid'=>'36'),'createtime DESC');
		$this->assign($link);
		$this->assign('catid',$catid);
		$this->assign ($cat);
		$this->assign('bcid',$bcid);
		$this->assign ("vo",$data);
        $this->display($template);	 
	}//end detail
	
	/**
	   *功能列表
	   *@date 2011-7-23
	   *@time 下午12:58:29
	   */
	function lst() {
		//功能列表
		$tid=$_REQUEST['tid'];
		$tour= M("Tour");;
		$tour_info = $tour->find($tid);
		$this->assign("vo",$tour_info);
		parent::index();
	}//end list
	/**
	   *功能列表
	   *@date 2011-7-23
	   *@time 下午12:58:29
	   */
	function show() {
		//功能列表
		$this->detail();
	}//end list
	
	function linkdata($model='Article',$condition=array(),$order="",$limit='0,20') {
		$dao=M($model);
		$arr=array();
		if($limit=='0,1'){
			$arr=$dao->where($condition)->order($order)->limit($limit)->find();
		}else{
			$arr=$dao->where($condition)->order($order)->limit($limit)->findAll();
		}
		return $arr;
	}
}